package de.bastian.hinterleitner;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import de.bastian.hinterleitner.database.DataHelper;
import de.hijacksoft.wyc.R;

public class LoadingActivity extends Activity {

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading);
		setRequestedOrientation(getResources().getConfiguration().orientation);
		new LoadTask().execute();
	}
	
	@SuppressWarnings("rawtypes")
	class LoadTask extends AsyncTask{
		
		@Override
		protected Object doInBackground(Object... arg0) {
			DataHelper.getDataHelper(LoadingActivity.this).close();
			DataHelper.getDataHelper(LoadingActivity.this).init();
			return null;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			SharedPreferences sp = getSharedPreferences("values", MODE_PRIVATE);
	    	String version = sp.getString("version", "0");
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
			finish();
	        try {
				if(!version.equals(getPackageManager().getPackageInfo(getPackageName(), 0).versionName))startActivity(new Intent(getApplicationContext(), WhatsNewActivity.class));
			} catch (NameNotFoundException e) {
			}
		}
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK))return false;
		return super.onKeyDown(keyCode, event);
	}
	
}
